﻿XPhoto.Util = (function () {
    var logger = null;
    var zoomFactor = null;

    return {
        isMsTouch: window.navigator && window.navigator.msPointerEnabled, //&& window.navigator.msMaxTouchPoints,
        isTouch: (function() {
           var el = document.createElement('div');
            el.setAttribute('ongesturestart', 'return;');
            if(typeof el.ongesturestart == "function"){
                return true;
            }else {
                return false
            }
        })(),

        apply: function (o, p) {
            for (var k in p) {
                o[k] = p[k];
            }
            return o;
        },

        getZoomFactor: function () {
            if (!zoomFactor) {
                zoomFactor = document.body.clientWidth / 1366
            }
            return zoomFactor;
        },
        getEventPosition: function (e) {
            if (this.isMsTouch) {
                return Kinetic.Global.pointerList[e.pointerId];
            } else {
                return PuppetPlay.StageScenery.getStage().mousePos;
            }
        },
        getPointerTarget: function (e) {
            return PuppetPlay.StageScenery.getStage()._getNodeById(Kinetic.Global.pointerMap[e.pointerId]);;
        },
		
        setDebugMode: function (boo) {
            if (!logger) {
                logger = document.getElementById('debugger');
            }
		    PuppetPlay.Setting.DEBUG_MODE = boo;
			logger.style.display = boo? 'block': 'none';
		},
		
		log: function(content){
		    logger.innerHTML = content;
		},

		round: function(n){
		    return (n + 0.5) | 0;
		}, 
		
        getPointAngle: function (position, center) {
            var x = position.x - center.x;
            var y = position.y - center.y;
            var angle = Math.atan(y / x);

            if (x >= 0) {
                if (y >= 0) {
                    return Math.atan(y / x);
                } else {
                    return Math.PI * 2 - Math.atan(-y / x);
                }
            } else {
                if (y >= 0) {
                    return Math.PI + Math.atan(y / x);
                } else {
                    return Math.PI + Math.atan(y / x);
                }
            }
        }
    };
})();
